package com.matei.eece411.A2;

import java.rmi.*;
import java.rmi.registry.*;
import java.net.*;

public class Main {
    
    static GUI gui;
    static MessageQueue _queue;
    
    public static void main(String[] args) {
        // create a shared buffer where the GUI add the messages thet need to 
        // be sent out by the main thread.  The main thread stays in a loop 
        // and when a new message shows up in the buffer it sends it out 
        // to the chat server (using RMI)
        _queue = new MessageQueue();
        
        // instantiate the GUI - in a new thread
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                gui = GUI.createAndShowGUI(_queue);  
                gui.jionChatRoom();
            }
        });
        
   
        
        
        // hack make sure the GUI instantioation is completed by the GUI thread 
        // before the next call
        while (gui == null)
            Thread.currentThread().yield();
        
        // calling the GUI method that updates the text area of the GUI
        // NOTE: you might want to call the same method when a new chat message 
        //       arrives
        gui.addToTextArea("RemoteUser:> Sample of displaying remote maessage");
       
        
        // The code below serves as an example to show how the shares message 
        // between the GUI and the main thread.
        // You will probably want to replace the code below with code that sits in a loop,  
        // waits for new messages to be entered by the user, and sends them to the 
        // chat server (using an RMI call)
        // 
        // In addition you may want to add code that
        //   * connects to the chat server and provides an object for callbacks (so 
        //     that the server has a way to send messages generated by other users)
        //   * implement the callback object which is called by the server remotely 
        //     and, in turn, updates the local GUI
        
        
        while (true) {
            String s;
            try {
                // wait until the user enters a new chat message
                s = _queue.dequeue();
            }
            catch (InterruptedException ie) {
                break;
            }
        
            // update the GUI with the message entered by the user
            gui.addToTextArea("Me:> " + s); 
            
            // print it to System.out (or send it to the RMI server)
            System.out.println ("User entered: " + s + " -- now sending it to chat server");
        } // end while loop
    }
   
}
